﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using PS.Config.Utils;

namespace PS.Config.Layouts.PS.Config
{
    public partial class Settings : LayoutsPageBase
    {
        private static readonly Dictionary<string, string> UserInput = new Dictionary<string, string>();
        private static int _selectedIndex;
        protected void Page_Init(object sender, EventArgs e)
        {
            CreateMenuItems();
            ShowPanelContent(_selectedIndex);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ApplyMenuImages();
        }

        private void ShowPanelContent(int index)
        {
            var config = Controller.Current.Sections[index];
            Panel_Main.Controls.Clear();
            foreach (var field in ReflectionHelper.EnumFields(config))
            {
                var divStart = new Literal { Text = "<div>" };
                var label = new Label { Text = field.Value.Name };
                var textBox = new TextBox { ID = field.Key };
                if (UserInput.ContainsKey(field.Key))
                {
                    textBox.Text = UserInput[field.Key];
                }
                else
                {
                    var val = field.Key.GetPropertyBagValue();
                    if (!string.IsNullOrEmpty(val))
                        textBox.Text = val;
                }
                var divEnd = new Literal { Text = "</div>" };

                Panel_Main.Controls.Add(divStart);
                Panel_Main.Controls.Add(label);
                Panel_Main.Controls.Add(textBox);
                Panel_Main.Controls.Add(divEnd);
            }
        }

        private void CreateMenuItems()
        {
            if (!IsPostBack)
            {
                foreach (var config in Controller.Current.Sections)
                {
                    var menuItem = new MenuItem(config.GetType().Name, Menu_Tabs.Items.Count.ToString());
                    Menu_Tabs.Items.Add(menuItem);
                }
                if (Menu_Tabs.Items.Count > 0)
                    Menu_Tabs.Items[0].Selected = true;
            }
        }


        private void ApplyMenuImages()
        {
            foreach (MenuItem menuItem in Menu_Tabs.Items)
            {
                menuItem.ImageUrl = menuItem.Selected ? @"/_layouts/images/ps.config/home_it02.gif" : @"/_layouts/images/ps.config/home_it01.gif";
            }
        }

        protected void Menu_Tabs_MenuItemClick(object sender, MenuEventArgs e)
        {
            SaveUserInput();
            _selectedIndex = Int32.Parse(e.Item.Value);
            ShowPanelContent(_selectedIndex);
            ApplyMenuImages();
        }

        private void SaveUserInput()
        {
            var controls = Panel_Main.Controls.OfType<TextBox>();
            foreach (var textBox in controls)
            {
                UserInput[textBox.ID] = textBox.Text;
            }
        }

        protected void Button_Update_Click(object sender, EventArgs e)
        {
            _selectedIndex = 0;

            SaveUserInput();
            foreach (var pair in UserInput)
            {
                pair.Key.SetPropertyBagValue(pair.Value);
            }

            //Resetting temporary local cache
            UserInput.Clear();

            //Reload values from site property bag
            Controller.Init();

            SPUtility.TransferToSuccessPage("Site settings updated successfully", @"/_layouts/settings.aspx", null, null);
        }
    }
}
